home *** CD-ROM | disk | FTP | other *** search
- /* ======================================================================
- OBJECT: com_netobjects_Calendar
-
- DESC: This object creates an interactive calendar. Developers can specify
- a selected date and render a calendar for that month. Developers write
- custom functions to populate the calendar with data or respond to user clicks.
- Users can navigate the calendar and select a specific date.
-
- TESTED
- PLATFORMS: Netscape Navigator 4.05
- Microsoft Internet Explorer 4.01
- ====================================================================== */
- function com_netobjects_Calendar(params) {
-
- // Public Properties
- this.name = (params.name != null) ? params.name : "";
- this.height = (params.height != null) ? params.height : "";
- this.width = (params.width != null) ? params.width : "";
-
- this.selDate = (params.selDate != null) ? params.selDate : "";
- this.wdayFormat = (params.wdayFormat != null) ? params.wdayFormat : "FirstLetter";
- this.textFont = (params.textFont != null) ? params.textFont : "Arial";
- this.textSize = (params.textSize != null) ? params.textSize : "12";
- this.textColor = (params.textColor != null) ? params.textColor : "";
- this.selColor = (params.selColor != null) ? params.selColor : "";
- this.border = ((params.border != null) && (params.border != "")) ? params.border : "0";
- this.leftButton = (params.leftButton != null) ? params.leftButton : "images/leftbutton.gif";
- this.rightButton = (params.rightButton != null) ? params.rightButton : "images/rightbutton.gif";
- this.invisibleButton = (params.leftButton != null) ? params.leftButton : "images/invisibleleftbutton.gif";
- this.testMode = ((params.testMode != null) && (params.testMode != "")) ? params.testMode : "off"
- this.curYear = null // current selected year
- this.curMonth = null // current selected month
- this.curDay = null // current selected day
- this.dirMode = (params.dirMode != null) ? params.dirMode : false; // direction Mode; default foreward from BaseDate
-
- // Private Properties
- this.childWindow = null // Handle to popup window created on open
- this.RenderingDay = null // Day currently being rendered - used typically in onRenderDay event
- this.assocDateField = null // Associated date field
-
- // Public Methods
- this.getSelDate = _Calendar_getSelDate; // Returns the last selected date as a string in the form mm/dd/yy
- this.render = _Calendar_render; // Renders selected month on the spot
- this.open = _Calendar_open; // Renders selected month in new window
-
- // Private Methods
- this.renderMonth = _Calendar_RenderMonth;
- this.validateProperties= _Calendar_ValidateProperties;
- // --- date utilities
- this.monthDays = _Calendar_MonthDays;
- this.getMonths = _Calendar_GetMonths;
- this.getWDays = _Calendar_GetWDays;
- this.parseDate = _Calendar_ParseDate;
- this.dateIncMonth = _Calendar_DateIncMonth;
-
-
- // Public Events
- this.onSelDate = _Calendar_onSelDate; // on user selection of date or change of month
- this.onRender = _Calendar_onRender; // on render of calendar
- this.onRenderDay = _Calendar_onRenderDay; // on render of each calendar day
- this.onOpen = _Calendar_onOpen; // on open of new window & render of calendar
-
- // Private Events (internal event driven functionality)
- this.eonSelDate = _Calendar_eonSelDate;
-
- // Validate object properties
- this.validateProperties();
-
- // Public Methods ----------------------------------------
-
- /* ======================================================================
-
- METHOD: _Calendar_render. Renders calendar control.
-
- ====================================================================== */
- function _Calendar_render() {
- // Initialize Variables
-
- var theDate = "";
- if (this.selDate != "")
- theDate = this.parseDate(this.selDate)
- else {
- var today = new Date();
- var todayStr = today.getMonth() + 1;
- todayStr += "/" + today.getDate();
- todayStr += "/" + today.getYear();
- theDate = this.parseDate(todayStr);
- }
-
- if (theDate != null) {
- this.renderMonth(theDate.longYear,theDate.month-1,theDate.day);
-
- // Fire public events
- this.onRender();
-
- } else {
- alert(this.selDate + " is not a valid date")
- }
-
-
- } // END _Calendar_render
-
-
- /* ======================================================================
-
- METHOD: _Calendar_open. Pops up a separate window and renders calendar control.
-
- ====================================================================== */
- function _Calendar_open(dfobj) {
- // Initialize Variables
- var sheight = ""
- var swidth = ""
-
- // Set Variables
- if (this.height != "") sheight = ",height=" + this.height
- if (this.width != "") swidth = ",width=" + this.width
-
- // Write HTML
- this.childWindow = document.all[dfobj.calendar];
- this.assocDateField = dfobj;
- this.render();
-
- // Fire public events
- this.onOpen();
-
- } // END _Calendar_open
-
- /* ======================================================================
-
- METHOD: _Calendar_getSelDate. Pops up a separate window and
- renders calendar control.
-
- ====================================================================== */
- function _Calendar_getSelDate() {
- return this.selDate;
- } // END _Calendar_getSelDate
-
-
- // Private Methods ----------------------------------------
-
- /* ======================================================================
-
- METHOD: _Calendar_ValidateProperties. Validate current properties.
-
- ====================================================================== */
- function _Calendar_ValidateProperties() {
-
- if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
- var errors = new Array()
- iIndex = 0;
-
- // Check for errors
- if (this.name == "")
- errors[iIndex++] = "Name is a required property.";
-
- // Display errors
- for (var i=0; i<errors.length; i++) {
- if (this.testMode.toLowerCase() == "text")
- document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_Calendar, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n")
- else
- alert("TEST MODE, com_netobjects_Calendar, '" + this.name + "' : " + errors[i]);
-
- }
- }
- } // END _Calendar_ValidateProperties
-
- /* ======================================================================
-
- METHOD: _Calendar_MonthDays. Returns days in selected month / year.
-
- ====================================================================== */
- function _Calendar_MonthDays(month, yr) {
- var Days = new Array();
-
- Days[0] = 31;
- Days[1] = 28;
- Days[2] = 31;
- Days[3] = 30;
- Days[4] = 31;
- Days[5] = 30;
- Days[6] = 31;
- Days[7] = 31;
- Days[8] = 30;
- Days[9] = 31;
- Days[10] = 30;
- Days[11] = 31;
-
- // Leap Year Rule:
- // If the year is evenly divisible by 4 AND NOT by 100, it's a leap year.
- // If the year is evenly divisible by 4 AND 100, it MUST also be divisible by 400
- // to be a leap year. Otherwise, it is not a leap year.
- var isLeap = false;
- isLeap = ((yr % 400 == 0) || (( yr % 4 == 0) && (yr % 100 != 0)));
-
- if ((month == 1) && (isLeap)) {return Days[month] + 1} else {return Days[month]};
-
- } // END _Calendar_MonthDays
-
- /* ======================================================================
-
- METHOD: _Calendar_GetMonths. Returns array of month names.
-
- ====================================================================== */
- function _Calendar_GetMonths(){
- this[0] = "January";
- this[1] = "February";
- this[2] = "March";
- this[3] = "April";
- this[4] = "May";
- this[5] = "June";
- this[6] = "July";
- this[7] = "August";
- this[8] = "September";
- this[9] = "October";
- this[10] = "November";
- this[11] = "December";
- } // END _Calendar_GetMonths
-
- /* ======================================================================
-
- METHOD: _Calendar_GetWDays. Returns array of days in specified format.
-
- ====================================================================== */
- function _Calendar_GetWDays(NoLtrs){
- if (NoLtrs=="FirstLetter") {
- this[0] = "S";
- this[1] = "M";
- this[2] = "T";
- this[3] = "W";
- this[4] = "Th";
- this[5] = "F";
- this[6] = "S";
- } else if (NoLtrs=="FullWord") {
- this[0] = "Sunday";
- this[1] = "Monday";
- this[2] = "Tuesday";
- this[3] = "Wednesday";
- this[4] = "Thursday";
- this[5] = "Friday";
- this[6] = "Saturday";
- } else { // Abbreviated
- this[0] = "Sun";
- this[1] = "Mon";
- this[2] = "Tues";
- this[3] = "Wed";
- this[4] = "Thurs";
- this[5] = "Fri";
- this[6] = "Sat";
- }
- } // END _Calendar_GetWDays
-
- /* ======================================================================
-
- METHOD: _Calendar_DateIncMonth. Increments (or Decrements) a date by month increments.
-
- ====================================================================== */
- function _Calendar_DateIncMonth(mm, dd, yy, incMo) {
- var dateObj = new Object();
-
- dateObj.month = mm + incMo;
- dateObj.year = yy, 10;
- dateObj.day = dd;
-
- if (dateObj.month > 11) {
- dateObj.month = 0
- dateObj.year = dateObj.year + 1
- } else if (dateObj.month < 0) {
- dateObj.month = 11
- dateObj.year = dateObj.year - 1
- }
-
- var dayCount = this.monthDays(dateObj.month, dateObj.year);
- if (dateObj.day > dayCount) dateObj.day = dayCount;
-
- return dateObj
-
- } // END _Calendar_DateIncMonth
-
- /* ======================================================================
-
- METHOD: _Calendar_ParseDate. Parses a date string in m/d/y or mm/dd/yy
- or mm/dd/yyyy format into it's month, day, year parts. Returns
- object with properties: month, day, year.
- ====================================================================== */
- function _Calendar_ParseDate(mdyStr) {
- var isValid = true
- var sep1Loc = null
- var sep2Loc = null
-
- //Validate string and get two separator locations
- if (mdyStr == null)
- isValid = false;
- else {
- /* loop through the string and test each character */
- for (i = 0; i < mdyStr.length; i++) {
- /* the character must be a number, a forward slash or a dash */
- if (!((mdyStr.charAt(i) >= '0') && (mdyStr.charAt(i) <= '9') ||
- (mdyStr.charAt(i) == '/') || (mdyStr.charAt(i) == '-'))) {
- isValid = false;
- break;
- } else if ((mdyStr.charAt(i) == '/') || (mdyStr.charAt(i) == '-')) {
- if (sep1Loc == null) {
- sep1Loc = i
- } else {
- if (sep2Loc == null) {
- sep2Loc = i
- } else { // Too many separators found!
- isValid = false;
- break;
- }
- }
- }
- } // end for loop
- }
-
- // Must have found 2 separators
- if (sep2Loc == null) {isValid = false}
-
- if (isValid) {
- myObj = new Object();
- myObj.month = mdyStr.substring(0, sep1Loc);
- myObj.day = mdyStr.substring(sep1Loc + 1, sep2Loc);
- myObj.year = mdyStr.substring(sep2Loc+1, mdyStr.length);
- myObj.longYear = parseInt(myObj.year, 10)
- if (!isNaN(myObj.longYear)) {
- if (myObj.longYear < 100) {myObj.longYear = myObj.longYear+1900}
- } else {
- myObj.longYear = 0
- }
-
- return myObj
- } else {
- return null;
- }
-
- } // END _Calendar_ParseDate
-
-
- /* ======================================================================
-
- METHOD: _Calendar_RenderMonth. Renders a selected year month, highlighting
- selected day if selDay = 0, does not highlight
-
- ====================================================================== */
- function _Calendar_RenderMonth( selYear, selMonth, selDay) {
-
- var doc = this.childWindow;
- var startDay = (new Date(selYear,selMonth,1)).getDay();
- var dayCount = this.monthDays(selMonth, selYear);
- var calWeeks = Math.ceil((startDay + dayCount)/7)
- var wdays = new this.getWDays(this.wdayFormat);
- var cnt = 1
- // Specify height, if given
- var cellHeight = ""
- if (this.height != "")
- " height=" + Math.floor(this.height/10);
-
- // Specify text color, if given
- var stextColor = ""
- if (this.textColor != "") stextColor = "link=\"" + this.textColor + "\""
-
- // Specify font face and size, if given
- var sfontTag = ""
- if (this.textFont != "") sfontTag = sfontTag + " Face=\"" +this.textFont + "\""
- //if (this.textSize != "") sfontTag = sfontTag + " Size=\"" +this.textSize +"\""
- if (this.textSize != "") sfontTag = sfontTag + " Style='font-size:9pt'"
-
- if (sfontTag != "") sfontTag = "<Font " + sfontTag + " >"
-
- this.curYear = selYear
- this.curMonth = selMonth
- this.curDay = selDay
-
- doc.innerHTML = ""; // clear the calendar window
- hst = "<table width='100%' Border=\"" + this.border +"\">"
-
- // Check sel day for existance in previous month
- var prevDate = this.dateIncMonth(selMonth,selDay,selYear, -1)
- var nextDate = this.dateIncMonth(selMonth,selDay,selYear, 1)
-
- // Write month - year title
- hst += "<tr><td colspan=7 align=center " + cellHeight + "><b>" + sfontTag
- hst += "<TABLE WIDTH=100%><tr>";
-
- // Write previous month button
- hst += "<td align=left>"
- if ( selYear == baseYear && selMonth == baseMonth && this.dirMode == false ) {
- hst += '<IMG SRC="' + this.invisibleButton + '" BORDER=0 ALIGN=TOP >'; // no previous Month if current month = BaseMonth
- }
- else {
- hst += '<IMG SRC="' + this.leftButton + '" BORDER=0 ALIGN=TOP onClick="' + this.name + '.eonSelDate(' + prevDate.month + ',' + prevDate.day + ',' + prevDate.year + ')" ALT="Prev">';
- }
- hst += "</td>";
-
- // Write month - year title
- hst += "<td align=center><b>" + sfontTag;
- hst += (new this.getMonths())[selMonth] + " " + selYear;
- hst += "</b></font></td>"
-
- // Write next month button
- hst += "<td align=right>";
- if ( selYear == baseYear && selMonth == baseMonth && this.dirMode == true ) {
- hst += '<IMG SRC="' + this.invisibleButton + '" BORDER=0 ALIGN=TOP >'; // no next Month if current month = BaseMonth
- }
- else {
- hst += '<IMG SRC="' + this.rightButton + '" BORDER=0 ALIGN=TOP onClick="' + this.name + '.eonSelDate(' + nextDate.month + ',' + nextDate.day + ',' + nextDate.year + ')" ALT="Next">';
- }
- hst += '</td>';
- hst += "</tr></TABLE></tr>";
-
- // Write day titles
- hst += "<tr>"
- for (var k = 0; k < 7; k++) {
- hst += "<td align=center" + cellHeight + " bgcolor=silver >" + sfontTag
- hst += wdays[k]
- hst += "</font></td>"
- }
- hst += "</tr>"
-
- // Write days
- for (var i = 0; i < calWeeks; i++) {
- hst += "<tr>"
- for (var j = 0; j < 7; j++) {
- countDay = (((i > 0) || (j >= startDay)) && (cnt <= dayCount))
-
- if ((selDay != 0) && (selDay == cnt) && (countDay)) {
- var sSelColor = ""
- if (this.selColor != "") {sSelColor = " BGCOLOR = \"" +this.selColor + "\""}
- hst += "<td align=\"center\" " + sSelColor + cellHeight + " >"
- } else {
- hst += "<td align=\"center\" valign=top" + cellHeight + " >"
- }
-
- if (countDay) {
- sfT = sfontTag.substring(0,sfontTag.length-1)
- hst += sfT
- if ( selYear == baseYear && selMonth == baseMonth && this.dirMode == false && cnt < baseDay ) {
- hst += ' '
- }
- else if ( selYear == baseYear && selMonth == baseMonth && this.dirMode == true && cnt > baseDay ) {
- hst += ' '
- }
- else {
- hst += ' style="cursor:hand; color:blue; text-decoration:underline"'
- }
- hst += ' onClick="' + this.name + '.eonSelDate(' + selMonth + ',' + cnt + ',' + selYear + ')">'
- hst += cnt
- hst += "</Font>"
- this.RenderingDay = cnt;
- this.onRenderDay();
- cnt = cnt + 1;
- }
- hst += "</td>"
- }
- hst += "</tr>"
- }
-
- hst += "</tr><td colspan=\"7\" align=center>"
-
- // Check sel day for existance in previous month
- var prevDate = this.dateIncMonth(selMonth,selDay,selYear, -1)
- var nextDate = this.dateIncMonth(selMonth,selDay,selYear, 1)
-
- hst += "</td></tr>"
- hst += "</table>"
- doc.insertAdjacentHTML("beforeEnd",hst); // output the calendar
- } // END _Calendar_RenderMonth
-
- // Private Event Handlers ----------------------------------------
-
- /* ======================================================================
-
- EVENT: _Calendar_eonSelDate. Internal event handler. Sets selDate and
- re-renders control.
-
- ====================================================================== */
- function _Calendar_eonSelDate(selMonth, selDay, selYear) {
- // Set the selected date
- sMon = (selMonth+1);
- //sMon = (sMon < 10) ? "0"+sMon : ""+sMon;
- sDay = (selDay < 10) ? "0"+selDay : ""+selDay;
-
- this.selDate = sMon + "/" + sDay + "/" + selYear
-
- // Render the month
- if ( this.onSelDate(selMonth, selDay, selYear, this.assocDateField, this.dirMode) ) {
- if ( selMonth == curSelMonth && selYear == curSelYear) {
- selDay = curDay;
- }
- else {
- selDay = 0;
- }
- this.renderMonth(selYear,selMonth,selDay);
- // Fire public events
- this.onRender();
- }
- } // END _Calendar_eonSelDate
-
-
- // Public Event Handlers ----------------------------------------
- function _Calendar_onRender() {
- }
- function _Calendar_onRenderDay() {
- }
- function _Calendar_onSelDate() {
- }
- function _Calendar_onOpen() {
- }
-
- } // END CONSTRUCTOR com_netobjects_Calendar